package com.hzg.lanya.frame;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.hzg.lanya.R;

public class ConnectedFragment extends Fragment implements View.OnClickListener {
    private final String TAG = "ConnectedFragment";
    private String address = "";

    private TextView temperatureText;
    private TextView dateText;

    private static final int REQ_HEX_INPUT = 3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        zcMyGb();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_connected, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        temperatureText = view.findViewById(R.id.temperature_text);
        dateText = view.findViewById(R.id.date_text);
    }

    public void displayRxData(String dataStr) {
        // Check if data is valid
        if (dataStr != null && dataStr.length() >= 16) {
            String host = dataStr.substring(0, 4);

            if (host.equals("A0AA")) {
                // Update the TextViews if the time and temperature are set successfully
                String temperature = "38.7°C";  // This should come from your data parsing logic
                String date = "2015/07/20\n23:50"; // This should come from your data parsing logic
                setTextTable("设置时间成功");
                updateTemperatureAndDate(temperature, date);
            } else if (host.equals("A0A1")) {
                setTextTable("设置电流成功");
            } else {
                setTextTable("失败，未知指令:" + dataStr);
            }
        } else {
            setTextTable("失败:" + dataStr);
        }
    }

    private void updateTemperatureAndDate(String temperature, String date) {
        // Update the temperature and date in the UI
        temperatureText.setText(temperature);
        dateText.setText(date);
    }

    private void setTextTable(String str) {
        // This can be used to update some status message in a separate TextView
        // nametable.setText(name + "      " + str);
    }

    // Implement the onClick method to satisfy the OnClickListener interface
    @Override
    public void onClick(View v) {
        // Handle your click events here, or leave it empty if not needed
    }

    // Other methods (onClick, etc.) remain unchanged

    private void zcMyGb() {
        IntentFilter intentFilter = new IntentFilter(MyConfig.MY_MSG_GB);
        getActivity().registerReceiver(new BootCompleteReceiver(), intentFilter);
    }

    public class BootCompleteReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String a = intent.getAction();
            if (a.equals(MyConfig.MY_MSG_GB)) {
                String name = intent.getStringExtra("key");
                // Process received data here if needed
            }
        }
    }
}
